package com.cn.wanxi.util;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * 如果很多地方都要频繁调用当前类里面的方法，一般情况下用static
 * 如果项目不是必须要用到IO，就不用static
 * 如果当前类里面的代码和自身无关，就应该提取出去
 */
public class IO {
    private static final String SPLIT = "--";

    private static File file = null;

    /**
     * 根据路径和文件名创建文件
     *
     * @param directory
     * @param fileName
     * @return
     */
    public static File createFile(String path, String newDirectory, String fileName) {
//        局部变量和全局遍历 如果变量名相同，采取就近原则
        File directory = createDirectory(path, newDirectory);
        File saveFile = new File(directory, fileName);
        try {
            saveFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        file = saveFile;
        return file;
    }

    /**
     * 根据路径创建文件夹
     *
     * @param path
     * @param newDirectory
     * @return
     */
    private static File createDirectory(String path, String newDirectory) {
        File file = new File(path, newDirectory);
        file.mkdirs();
        return file;
    }

    /**
     * 根据文件和数据进行写入
     *
     * @param file
     * @param data
     */
    public static void wireteData(String[] data) {
        try {
            OutputStream outputStream = new FileOutputStream(file);

            for (int i = 0; i < data.length; i++) {
                outputStream.write(data[i].getBytes(StandardCharsets.UTF_8));
                outputStream.write(SPLIT.getBytes(StandardCharsets.UTF_8));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据路径读取文件里面的数据
     *
     * @param file
     * @return
     */
    public static String readData() {
        StringBuilder data = new StringBuilder();
//        String和StringBuilder和StringBuffer的区别？
//        String a="aa";
//        String b="bb";
//        String c=a+b;
//        String d="aa"+"bb";
//        c.equals(d)为true和为false?
//        c和d分别创建了几个对象？
//        String e=new String("aa");
//        e和a是否相等？
//        字符流和字节流的区别?
        try {
//            InputStream inputStream = new FileInputStream(file);
//
////            循环遍历,要遍历多少次？
////            int ---char
//            long count = file.length();
//            for (int i = 0; i < count; i++) {
//                System.out.print((char) inputStream.read());
//            }
//            需要用字符流读
            InputStream is = new FileInputStream(file);
            InputStreamReader reader = new InputStreamReader(is, StandardCharsets.UTF_8);
//            Reader reader = new FileReader(file);
            int result = 0;

            while ((result = reader.read()) != -1) {
//                result = reader.read();
//                System.out.print((char) result);
//                data += (char) result + "";
                data.append((char) result);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
//        System.out.println(data);
        return data.toString();
    }
}
